package com.sims.daoImp;

import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.sims.bean.CourseInfo;
import com.sims.dao.ICourseInfoDAO;

/**
 * A data access object (DAO) providing persistence and search support for
 * CourseInfo entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.sims.bean.CourseInfo
 * @author MyEclipse Persistence Tools
 */

public class CourseInfoDAO extends HibernateDaoSupport implements ICourseInfoDAO {
	private static final Logger log = LoggerFactory
			.getLogger(CourseInfoDAO.class);
	// property constants
	public static final String TID = "tid";
	public static final String CID = "cid";
	public static final String COURSE = "course";

	@Override
	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.ICourseInfoDAO#save(com.sims.bean.CourseInfo)
	 */
	@Override
	public void save(CourseInfo transientInstance) {
		log.debug("saving CourseInfo instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.ICourseInfoDAO#delete(com.sims.bean.CourseInfo)
	 */
	@Override
	public void delete(CourseInfo persistentInstance) {
		log.debug("deleting CourseInfo instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.ICourseInfoDAO#findById(java.lang.Integer)
	 */
	@Override
	public CourseInfo findById(java.lang.Integer id) {
		log.debug("getting CourseInfo instance with id: " + id);
		try {
			CourseInfo instance = (CourseInfo) getHibernateTemplate().get(
					"com.sims.bean.CourseInfo", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.ICourseInfoDAO#findByExample(com.sims.bean.CourseInfo)
	 */
	@Override
	public List findByExample(CourseInfo instance) {
		log.debug("finding CourseInfo instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.ICourseInfoDAO#findByProperty(java.lang.String, java.lang.Object)
	 */
	@Override
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding CourseInfo instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from CourseInfo as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.ICourseInfoDAO#findByTid(java.lang.Object)
	 */
	@Override
	public List findByTid(Object tid) {
		return findByProperty(TID, tid);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.ICourseInfoDAO#findByCid(java.lang.Object)
	 */
	@Override
	public List findByCid(Object cid) {
		return findByProperty(CID, cid);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.ICourseInfoDAO#findByCourse(java.lang.Object)
	 */
	@Override
	public List findByCourse(Object course) {
		return findByProperty(COURSE, course);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.ICourseInfoDAO#findAll()
	 */
	@Override
	public List findAll() {
		log.debug("finding all CourseInfo instances");
		try {
			String queryString = "from CourseInfo";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.ICourseInfoDAO#merge(com.sims.bean.CourseInfo)
	 */
	@Override
	public CourseInfo merge(CourseInfo detachedInstance) {
		log.debug("merging CourseInfo instance");
		try {
			CourseInfo result = getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.ICourseInfoDAO#attachDirty(com.sims.bean.CourseInfo)
	 */
	@Override
	public void attachDirty(CourseInfo instance) {
		log.debug("attaching dirty CourseInfo instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.ICourseInfoDAO#attachClean(com.sims.bean.CourseInfo)
	 */
	@Override
	public void attachClean(CourseInfo instance) {
		log.debug("attaching clean CourseInfo instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static ICourseInfoDAO getFromApplicationContext(ApplicationContext ctx) {
		return (ICourseInfoDAO) ctx.getBean("CourseInfoDAO");
	}
}